<?php
namespace app\backstage\logic;
use think\Db;
class Version
{
    /*列表数据获取*/
    public function getList($params)
    {
        $field = 'id,quick_id,logo,channel_id,version,download_path,create_time,update_time,force_update,hot_update';

        $condition[] = ['version', 'like', 'require','version'];
        $condition[] = ['channel_id', 'eq', 'gt:0','channel_id'];
        $condition[] = ['quick_id', 'eq', 'gt:0','quick_id'];

        $where   = buildQueryCondition($condition, $params);

        if( isset($params['field']) ){
            $orderBy = [ $params['field'] => $params['order'] ];
        }else{
            $orderBy = ['id' => 'DESC'];
        }

        $Version = \think\facade\App::model('Feature');
        $list = $Version->getList($where, $field, $params['page'], $params['limit'], $orderBy);

        if($list){

            $quick_id_gh = array_unique(array_column($list,'quick_id'));
            $apps = Db::table('quick_app')->where('id','in',$quick_id_gh)->column('id,name');

            $channel_id_gh = array_unique(array_column($list,'channel_id'));
            $channels = Db::table('cms_manager_user')->where('id','in',$channel_id_gh)->column('id,account,channel_code');

            foreach($list as $k=>$v)
            {
                $list[$k]['app_name']               = isset($apps[$v['quick_id']]) ? $apps[$v['quick_id']] : '';
                $list[$k]['channel_name']           = isset($channels[$v['channel_id']]) ? $channels[$v['channel_id']]['account'] : '';
                $list[$k]['channel_code'] = isset($channels[$v['channel_id']]) ? $channels[$v['channel_id']]['channel_code'] : '';

                if(strlen($v['download_path']) > 1)
                {
                    $list[$k]['download_path'] = config('img_domain').$v['download_path'];
                    $list[$k]['apk_name'] = substr($v['download_path'], strrpos($v['download_path'], '/') + 1, strlen($v['download_path']));
                }else{
                    $list[$k]['download_path'] = '';
                    $list[$k]['apk_name'] = '';
                }
            }
        }

        $result['count']    = $Version->getTotalNumber($where);
        $result['list']     = $list;
        return $result;
    }

    /*数据获取*/
    public function getInfo($where)
    {
        $field = 'id,quick_id,logo,channel_id,version,download_path,force_update,hot_update,brief';
        $Version = \think\facade\App::model('Feature');

        $info = $Version->getOne($where,$field);

        return $info;
    }

    /*数据添加*/
    public function addInfo($data)
    {
        $Version = \think\facade\App::model('Feature');
        $res = $Version->insertRecord($data);

        if($res){
            return ['code'=>1,'msg'=>'操作成功'];
        }

        return ['code'=>0,'msg'=>'操作失败'];
    }

    /*数据编辑*/
    public function editInfo($where,$data)
    {
        $Version = \think\facade\App::model('Feature');
        $res = $Version->updateRecord($where,$data);

        if($res){
            return ['code'=>1,'msg'=>'操作成功'];
        }

        return ['code'=>0,'msg'=>'操作失败'];
    }

    /*数据删除*/
    public function delInfo($where)
    {
        $Version = \think\facade\App::model('Feature');
        $res = $Version->deleteRecord($where);

        if($res){
            return ['code'=>1,'msg'=>'操作成功'];
        }

        return ['code'=>0,'msg'=>'操作失败'];
    }

    /*数据处理*/
    public function dataProcessing($params)
    {

        if(isset($params['version']) && strlen($params['version']) >= 1)
        {
            $data['version']      = $params['version'];
        }

        $data['brief']          = isset($params['brief']) ? $params['brief'] : '';
        $data['hot_update']     = isset($params['hot_update']) ? $params['hot_update'] : '';
        $data['force_update']   = isset($params['force_update']) ? $params['force_update'] : '';
        $data['quick_id']       = isset($params['quick_id']) ? $params['quick_id'] : '';
        $data['channel_id']     = isset($params['channel_id']) ? $params['channel_id'] : '';

        if(isset($params['download_path']) && strlen($params['download_path']) > 1)
        {
            $data['download_path'] = $params['download_path'];
        }

        if(isset($params['logo']) && strlen($params['logo']) > 1)
        {
            $data['logo'] = $params['logo'];
        }

        return ['data'=>$data];
    }


}